package com.leyou.authAgain.controller;

import com.leyou.auth.pojo.UserInfo;
import com.leyou.auth.utils.JwtUtils;
import com.leyou.authAgain.config.JwtProperties;
import com.leyou.authAgain.service.AuthService;
import com.leyou.common.utils.CookieUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by 冯金平 on 2023/3/18 15:15
 */
@Controller
public class AuthController {


    @Autowired
    private AuthService authService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 身份验证
     * 这里因为将POST 转为get远程调用接口-feign调用不规范400，因此修改
     * 总之feign调用处和被调用处以及API请求方式必须一致否则出错，都是用户登录提交我修改都POST400
     *  因此改get
     *  ZOOL网关鉴权
     * @param username 用户名
     * @param password 密码
     * @param request  请求
     * @param response 响应
     *
     * @return {@link ResponseEntity}<{@link Void}>
     */
    @PostMapping("accredit")
    public ResponseEntity<Void> accredit(@RequestParam("username") String username,
                                         @RequestParam("password")String password, 
                                         HttpServletRequest request, 
                                         HttpServletResponse response){

        String token = this.authService.authentication(username,password);
        if (StringUtils.isBlank(token)){
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();//未授权返回状态码
        }
        CookieUtils.setCookie(request,response,jwtProperties.getCookiename(),token,jwtProperties.getExpire()*60);
        //token通过cookie存放，过期时间与token超时一致，token设置单位为分钟，cookie为秒
        return ResponseEntity.ok(null);//响应状态码，无结果登录
    }

    /**
     * 校验用户身份，必须是授权用户，否则登录，登录状态才可以下单
     * @param token
     * @return {@link ResponseEntity}<{@link UserInfo}>
     */
    @GetMapping("verify")
    public ResponseEntity<UserInfo>  verifyUser(@CookieValue("LY_TOKEN")String token,
                                                HttpServletRequest request,
                                                HttpServletResponse response){
        //载荷中解析用户信息
        try {
            UserInfo userInfo = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
            if (userInfo == null){
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
            }
            //刷新JWT有效时间
            token = JwtUtils.generateToken(userInfo,this.jwtProperties.getPrivateKey(),this.jwtProperties.getExpire());
            //过期时间为分钟
            //刷新COOKIE有效时间
            CookieUtils.setCookie(request,response,
                    this.jwtProperties.getCookiename(),
                    token,
                    this.jwtProperties.getExpire()*60);
            //过期时间为秒

        //返回用户信息，显示登录状态用户名
            return ResponseEntity.ok(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //服务内部错误
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
    }

    /**
     * 测试工程是否可以被正确的访问到tt
     *
     * @return {@link ResponseEntity}<{@link Void}>
     */
    @GetMapping("tt")
    public ResponseEntity<Void> tt(){
        return ResponseEntity.ok(null);
    }
}
